﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using ComponentFactory.Krypton.Toolkit;
using System.ComponentModel;

namespace LED_Utils.Controls {



	public static class DrawingMethods {
		public static void ClearTabSelectedBottomLine(Graphics g, Rectangle recBounds, Pen pen, TabAlignment Alignment) {
			switch (Alignment) {
				case TabAlignment.Top:
					g.DrawLine(pen, recBounds.Left + 1, recBounds.Bottom, recBounds.Right - 1, recBounds.Bottom);
					g.DrawLine(pen, (int)(recBounds.Left + 1), (int)(recBounds.Bottom + 1), (int)(recBounds.Right - 1), (int)(recBounds.Bottom + 1));
					return;

				case TabAlignment.Bottom:
					g.DrawLine(pen, recBounds.Left + 1, recBounds.Top, recBounds.Right - 1, recBounds.Top);
					g.DrawLine(pen, (int)(recBounds.Left + 1), (int)(recBounds.Top - 1), (int)(recBounds.Right - 1), (int)(recBounds.Top - 1));
					g.DrawLine(pen, (int)(recBounds.Left + 1), (int)(recBounds.Top - 2), (int)(recBounds.Right - 1), (int)(recBounds.Top - 2));
					return;

				case TabAlignment.Left:
					g.DrawLine(pen, recBounds.Right, recBounds.Top + 1, recBounds.Right, recBounds.Bottom - 1);
					g.DrawLine(pen, (int)(recBounds.Right + 1), (int)(recBounds.Top + 1), (int)(recBounds.Right + 1), (int)(recBounds.Bottom - 1));
					g.DrawLine(pen, (int)(recBounds.Right + 2), (int)(recBounds.Top + 1), (int)(recBounds.Right + 2), (int)(recBounds.Bottom - 1));
					g.DrawLine(pen, (int)(recBounds.Right + 3), (int)(recBounds.Top + 1), (int)(recBounds.Right + 3), (int)(recBounds.Bottom - 1));
					return;

				case TabAlignment.Right:
					g.DrawLine(pen, recBounds.Left, recBounds.Top + 1, recBounds.Left, recBounds.Bottom - 1);
					g.DrawLine(pen, (int)(recBounds.Left - 1), (int)(recBounds.Top + 1), (int)(recBounds.Left - 1), (int)(recBounds.Bottom - 1));
					g.DrawLine(pen, (int)(recBounds.Left - 2), (int)(recBounds.Top + 1), (int)(recBounds.Left - 2), (int)(recBounds.Bottom - 1));
					g.DrawLine(pen, (int)(recBounds.Left - 3), (int)(recBounds.Top + 1), (int)(recBounds.Left - 3), (int)(recBounds.Bottom - 1));
					return;
			}
		}

		public static Color ColorFromAhsb(int Alpha, float Hue, float Saturation, float Brightness) {
			float num;
			float num2;
			float num3;
			if ((0 > Alpha) || (0xff < Alpha)) {
				throw new ArgumentOutOfRangeException("a", Alpha, "InvalidAlpha");
			}
			if ((0f > Hue) || (360f < Hue)) {
				throw new ArgumentOutOfRangeException("h", Hue, "InvalidHue");
			}
			if ((0f > Saturation) || (1f < Saturation)) {
				throw new ArgumentOutOfRangeException("s", Saturation, "InvalidSaturation");
			}
			if ((0f > Brightness) || (1f < Brightness)) {
				throw new ArgumentOutOfRangeException("b", Brightness, "InvalidBrightness");
			}
			if (0f == Saturation) {
				return Color.FromArgb(Alpha, Convert.ToInt32((float)(Brightness * 255f)), Convert.ToInt32((float)(Brightness * 255f)), Convert.ToInt32((float)(Brightness * 255f)));
			}
			if (0.5 < Brightness) {
				num = (Brightness - (Brightness * Saturation)) + Saturation;
				num3 = (Brightness + (Brightness * Saturation)) - Saturation;
			} else {
				num = Brightness + (Brightness * Saturation);
				num3 = Brightness - (Brightness * Saturation);
			}
			int num4 = (int)Math.Floor((double)(Hue / 60f));
			if (300f <= Hue) {
				Hue -= 360f;
			}
			Hue /= 60f;
			Hue -= 2f * ((float)Math.Floor((double)(((num4 + 1f) % 6f) / 2f)));
			if ((num4 % 2) == 0) {
				num2 = (Hue * (num - num3)) + num3;
			} else {
				num2 = num3 - (Hue * (num - num3));
			}
			int green = Convert.ToInt32((float)(num * 255f));
			int red = Convert.ToInt32((float)(num2 * 255f));
			int blue = Convert.ToInt32((float)(num3 * 255f));
			switch (num4) {
				case 1:
					return Color.FromArgb(Alpha, red, green, blue);

				case 2:
					return Color.FromArgb(Alpha, blue, green, red);

				case 3:
					return Color.FromArgb(Alpha, blue, red, green);

				case 4:
					return Color.FromArgb(Alpha, red, blue, green);

				case 5:
					return Color.FromArgb(Alpha, green, blue, red);
			}
			return Color.FromArgb(Alpha, green, red, blue);
		}

		public static GraphicsPath CreateRectGraphicsPath(Rectangle rect) {
			GraphicsPath path = new GraphicsPath();
			path.AddRectangle(rect);
			return path;
		}

		public static void DrawContentFromOneColor(Graphics g, Rectangle rect, float baseHue, float Angle) {
			Color color = ColorFromAhsb(0xff, baseHue, 0.2958f, 0.7292f);
			Color color2 = ColorFromAhsb(0xff, baseHue, 0.5875f, 0.35f);
			Color color3 = ColorFromAhsb(0xff, baseHue, 0.7458f, 0.2f);
			Color color4 = ColorFromAhsb(0xff, baseHue, 0.6f, 0.4042f);
			Rectangle rectangle = new Rectangle(0, 0, rect.Width, rect.Height / 2);
			Rectangle rectangle2 = new Rectangle(0, rectangle.Height, rect.Width, (rect.Height - rectangle.Height) - 1);
			using (Brush brush = new LinearGradientBrush(rectangle, color, color2, Angle)) {
				g.FillRectangle(brush, rectangle);
			}
			using (Brush brush2 = new LinearGradientBrush(rectangle2, color3, color4, Angle)) {
				g.FillRectangle(brush2, rectangle2);
			}
		}

		public static void DrawGradient(Graphics g, Rectangle rect, Color DarkColor, Color LightColor, float Angle, bool EnableBorder, Color BorderColor, float BorderSize) {
			using (LinearGradientBrush brush = new LinearGradientBrush(rect, LightColor, DarkColor, Angle)) {
				Blend blend = new Blend(4);
				blend.Positions[0] = 0f;
				blend.Factors[0] = 0.9f;
				blend.Positions[1] = 0.4f;
				blend.Factors[1] = 0.5f;
				blend.Positions[2] = 0.4f;
				blend.Factors[2] = 0.2f;
				blend.Positions[3] = 1f;
				blend.Factors[3] = 1f;
				brush.Blend = blend;
				g.FillRectangle(brush, rect);
			}
			if (EnableBorder) {
				g.DrawRectangle(new Pen(BorderColor, BorderSize), rect);
			}
		}

		public static void DrawLinearGradientTwoParts(Graphics g, Rectangle rect, Color LightColor, Color MiddleColor1, Color MiddleColor2, Color DarkColor, float Angle) {
			Rectangle rectangle = new Rectangle();
			Rectangle rectangle2 = new Rectangle();
			rectangle = rect;
			rectangle2 = rect;
			rectangle.Height = rect.Height / 2;
			rectangle.Width = rect.Width;
			rectangle.X = rect.X;
			rectangle.Y = rect.Y;
			rectangle2.Height = rect.Height - rectangle.Height;
			rectangle2.Width = rect.Width;
			rectangle2.X = rect.X;
			rectangle2.Y = rect.Y + rectangle.Height;
			LinearGradientBrush brush = new LinearGradientBrush(rectangle, LightColor, MiddleColor1, LinearGradientMode.Vertical);
			LinearGradientBrush brush2 = new LinearGradientBrush(rectangle2, MiddleColor2, DarkColor, LinearGradientMode.Vertical);
			g.FillRectangle(brush, rectangle);
			g.FillRectangle(brush2, rectangle2);
		}

		public static void DrawListViewHeader(Graphics g, Rectangle rect, Color LightColor, Color DarkColor, float Angle) {
			if (DarkColor == Color.White) {
				DarkColor = Color.Snow;
			}
			using (Brush brush = GetBrush(rect, LightColor, DarkColor, PaletteColorStyle.Default, Angle, VisualOrientation.Top, false)) {
				g.FillRectangle(brush, rect);
			}
		}

		public static void DrawListViewHeader2(Graphics g, Rectangle rect, Color LightColor, Color MiddleColor, Color DarkColor, Color ExtraColor, float Angle) {
			int num = (rect.Height / 2) - 2;
			Rectangle rectangle = new Rectangle(rect.X, rect.Y + num, rect.Width, rect.Height - num);
			if ((MiddleColor == Color.White) && (DarkColor == Color.White)) {
				MiddleColor = Color.WhiteSmoke;
				DarkColor = Color.Snow;
			}
			using (LinearGradientBrush brush = new LinearGradientBrush(rect, LightColor, MiddleColor, Angle)) {
				g.FillRectangle(brush, rect);
			}
			using (LinearGradientBrush brush2 = new LinearGradientBrush(rectangle, MiddleColor, DarkColor, Angle)) {
				g.FillRectangle(brush2, rectangle);
			}
		}

		public static void DrawStatusBlend(Graphics g, Rectangle rect, Color LightColor, Color DarkColor, float Angle) {
			Blend blend = new Blend();
			blend.Positions = new float[] { 0f, 0.25f, 0.25f, 0.57f, 0.86f, 1f };
			blend.Factors = new float[] { 0.1f, 0.6f, 1f, 0.4f, 0f, 0.95f };
			RectangleF ef = new RectangleF(0f, 1.5f, (float)rect.Width, (float)(rect.Height - 2));
			if ((ef.Width > 0f) && (ef.Height > 0f)) {
				using (LinearGradientBrush brush = new LinearGradientBrush(ef, LightColor, DarkColor, 90f)) {
					brush.Blend = blend;
					g.FillRectangle(brush, ef);
				}
			}
		}

		public static void DrawTabHeader(Graphics g, Point[] pt, Rectangle rect, Color LightColor, Color MiddleColor, Color ExtraColor, float Angle, TabAlignment Ta, bool Extended, TabHeaderStatus Status, bool PreserveColor) {
			switch (Ta) {
				case TabAlignment.Top:
					Angle = 90f;
					break;

				case TabAlignment.Left:
					Angle = 0f;
					break;

				case TabAlignment.Right:
					Angle = 180f;
					if (Extended) {
						switch (Status) {
							case TabHeaderStatus.Selected:
								LightColor = ExtraColor;
								break;

							case TabHeaderStatus.HotSelected:
								LightColor = ExtraColor;
								break;
						}
					}
					break;
			}
			bool flag1 = MiddleColor == Color.White;
			if (Extended) {
				g.FillPolygon(new LinearGradientBrush(rect, MiddleColor, LightColor, Angle), pt);
			} else {
				g.FillPolygon(GetBrush(rect, LightColor, MiddleColor, PaletteColorStyle.Default, Angle, (VisualOrientation)Ta, PreserveColor), pt);
			}
		}

		public static void DrawTabHeader(Graphics g, GraphicsPath path, Rectangle rect, Color LightColor, Color MiddleColor, Color ExtraColor, float Angle, TabAlignment Ta, bool Extended, TabHeaderStatus Status, bool PreserveColor) {
			switch (Ta) {
				case TabAlignment.Top:
					Angle = 90f;
					break;

				case TabAlignment.Left:
					Angle = 0f;
					break;

				case TabAlignment.Right:
					Angle = 180f;
					if (Extended) {
						switch (Status) {
							case TabHeaderStatus.Selected:
								LightColor = ExtraColor;
								break;

							case TabHeaderStatus.HotSelected:
								LightColor = ExtraColor;
								break;
						}
					}
					break;
			}
			if (MiddleColor == Color.White) {
				MiddleColor = GetDarkerColor(Color.White);
			}
			if (Extended) {
				g.FillPath(new LinearGradientBrush(rect, MiddleColor, LightColor, Angle), path);
			} else {
				g.FillPath(GetBrush(rect, LightColor, MiddleColor, PaletteColorStyle.Default, Angle, (VisualOrientation)Ta, PreserveColor), path);
			}
		}

		public static void DrawTabHeader(Graphics g, PointF[] pt, Rectangle rect, Color LightColor, Color MiddleColor, Color DarkColor, Color ExtraColor, float Angle, TabAlignment Ta, bool Extended, TabHeaderStatus Status) {
			float num = (rect.Height / 2) - 2;
			PointF[] points = new PointF[5];
			switch (Ta) {
				case TabAlignment.Top:
					points[0] = pt[0];
					points[1] = new PointF(pt[0].X, pt[2].Y + num);
					points[2] = new PointF(pt[5].X, pt[3].Y + num);
					points[3] = pt[5];
					points[4] = pt[6];
					Angle = 90f;
					DarkColor = GetModifiedColor(DarkColor, 5, 0, 0);
					MiddleColor = GetModifiedColor(MiddleColor, -10, 0, 0);
					goto Label_04A2;

				case TabAlignment.Bottom:
					points[0] = new PointF(pt[5].X, pt[4].Y - num);
					points[1] = pt[0];
					points[2] = pt[1];
					points[3] = new PointF(pt[1].X, pt[3].Y - num);
					points[4] = new PointF(pt[5].X, pt[4].Y - num);
					Angle = 270f;
					DarkColor = GetModifiedColor(DarkColor, 10, 0, 0);
					MiddleColor = GetModifiedColor(MiddleColor, -10, 0, 0);
					goto Label_04A2;

				case TabAlignment.Left:
					num = (rect.Width / 2) - 2;
					points[0] = new PointF(pt[3].X + num, pt[2].Y);
					points[1] = new PointF(pt[4].X + num, pt[5].Y);
					points[2] = pt[0];
					points[3] = pt[1];
					points[4] = new PointF(pt[3].X + num, pt[2].Y);
					Angle = 0f;
					DarkColor = GetModifiedColor(DarkColor, 10, 0, 0);
					MiddleColor = GetModifiedColor(MiddleColor, -10, 0, 0);
					goto Label_04A2;

				case TabAlignment.Right:
					if (Extended) {
						points = new PointF[] { pt[0], pt[1], pt[2], pt[3], pt[4], pt[5], pt[6] };
						Angle = 180f;
						switch (Status) {
							case TabHeaderStatus.Normal:
								LightColor = DarkColor;
								DarkColor = GetModifiedColor(MiddleColor, 5, 0, 0);
								MiddleColor = LightColor;
								goto Label_04A2;

							case TabHeaderStatus.NormalPreserve:
								if (!(DarkColor == Color.White)) {
									DarkColor = GetModifiedColor(DarkColor, 5, 0, 0);
									break;
								}
								DarkColor = Color.WhiteSmoke;
								break;

							case TabHeaderStatus.Hot:
							case TabHeaderStatus.HotSelected:
								goto Label_04A2;

							case TabHeaderStatus.Selected:
								MiddleColor = ExtraColor;
								goto Label_04A2;
						}
					} else {
						num = (rect.Width / 2) - 2;
						points[0] = pt[1];
						points[1] = pt[0];
						points[2] = new PointF(pt[4].X - num, pt[5].Y);
						points[3] = new PointF(pt[3].X - num, pt[2].Y);
						points[4] = pt[1];
						Angle = 180f;
						DarkColor = GetModifiedColor(DarkColor, 10, 0, 0);
						MiddleColor = GetModifiedColor(MiddleColor, -10, 0, 0);
					}
					goto Label_04A2;

				default:
					goto Label_04A2;
			}
			MiddleColor = LightColor;
		Label_04A2:
			if ((MiddleColor == Color.White) && (DarkColor == Color.White)) {
				MiddleColor = Color.WhiteSmoke;
				DarkColor = Color.Snow;
			}
			using (LinearGradientBrush brush = new LinearGradientBrush(rect, LightColor, MiddleColor, Angle)) {
				g.FillPolygon(brush, pt, FillMode.Winding);
			}
			using (LinearGradientBrush brush2 = new LinearGradientBrush(rect, Color.FromArgb(180, Color.White), Color.FromArgb(20, Color.White), Angle)) {
				g.FillPolygon(brush2, pt, FillMode.Winding);
			}
			using (LinearGradientBrush brush3 = new LinearGradientBrush(rect, MiddleColor, DarkColor, Angle)) {
				g.FillPolygon(brush3, points);
			}
		}

		public static Brush GetBrush(Rectangle rect, Color ColorBegin, Color ColorEnd, PaletteColorStyle ColorStyle, float Angle, VisualOrientation orientation, bool PreserveColors) {
			Blend blend = new Blend(4);
			Blend blend12 = new Blend();
			blend12.Positions = new float[] { 0f, 0.25f, 0.25f, 0.57f, 0.86f, 1f };
			blend12.Factors = new float[] { 0.1f, 0.6f, 1f, 0.4f, 0f, 0.95f };
			blend.Positions[0] = 0f;
			blend.Factors[0] = 1f;
			blend.Positions[1] = 0.4f;
			blend.Factors[1] = 0.5f;
			blend.Positions[2] = 0.4f;
			blend.Factors[2] = 0.1f;
			blend.Positions[3] = 1f;
			blend.Factors[3] = 0.9f;
			float[] numArray = new float[4];
			numArray[2] = 1f;
			numArray[3] = 1f;
			Blend blend2 = new Blend();
			float[] numArray2 = new float[4];
			numArray2[2] = 1f;
			numArray2[3] = 1f;
			blend2.Factors = numArray2;
			blend2.Positions = new float[] { 0f, 0.33f, 0.33f, 1f };
			Blend blend3 = new Blend();
			float[] numArray3 = new float[4];
			numArray3[2] = 1f;
			numArray3[3] = 1f;
			blend3.Factors = numArray3;
			blend3.Positions = new float[] { 0f, 0.5f, 0.5f, 1f };
			Blend blend4 = new Blend();
			float[] numArray4 = new float[4];
			numArray4[3] = 1f;
			blend4.Factors = numArray4;
			blend4.Positions = new float[] { 0f, 0.9f, 0.9f, 1f };
			Blend blend5 = new Blend();
			blend5.Factors = new float[] { 0f, 0.5f, 1f, 0.05f };
			blend5.Positions = new float[] { 0f, 0.45f, 0.45f, 1f };
			Blend blend6 = new Blend();
			blend6.Factors = new float[] { 0f, 0f, 0.25f, 0.7f, 1f, 1f };
			blend6.Positions = new float[] { 0f, 0.1f, 0.2f, 0.3f, 0.5f, 1f };
			Blend blend7 = new Blend();
			blend7.Factors = new float[] { 0.15f, 0.75f, 1f, 1f };
			blend7.Positions = new float[] { 0f, 0.45f, 0.45f, 1f };
			Blend blend8 = new Blend();
			blend8.Factors = new float[] { 0.8f, 0.2f, 0f, 0.07f, 1f };
			blend8.Positions = new float[] { 0f, 0.33f, 0.33f, 0.43f, 1f };
			Blend blend9 = new Blend();
			blend9.Factors = new float[] { 1f, 0.7f, 0.7f, 0f, 0.1f, 0.55f, 1f, 1f };
			blend9.Positions = new float[] { 0f, 0.16f, 0.33f, 0.35f, 0.51f, 0.85f, 0.85f, 1f };
			Blend blend10 = new Blend();
			blend10.Factors = new float[] { 1f, 0.78f, 0.48f, 1f, 1f };
			blend10.Positions = new float[] { 0f, 0.33f, 0.33f, 0.9f, 1f };
			new Blend();
			Blend blend11 = new Blend();
			blend11.Factors = numArray;
			blend11.Positions = new float[] { 0f, 0.25f, 0.25f, 1f };
			if (ColorStyle == PaletteColorStyle.Default) {
				LinearGradientBrush brush;
				if (PreserveColors) {
					brush = new LinearGradientBrush(rect, ColorEnd, ColorBegin, Angle);
				} else {
					brush = new LinearGradientBrush(rect, GetDarkerColor(ColorEnd), GetLighterColor(ColorBegin), Angle);
				}
				brush.Blend = blend;
				return brush;
			}
			if (ColorStyle == PaletteColorStyle.Solid) {
				return new SolidBrush(ColorBegin);
			}
			switch (orientation) {
				case VisualOrientation.Bottom:
					Angle += 180f;
					break;

				case VisualOrientation.Left:
					Angle -= 90f;
					break;

				case VisualOrientation.Right:
					Angle += 90f;
					break;
			}
			if (ColorStyle == PaletteColorStyle.OneNote) {
				ColorBegin = Color.White;
			}
			LinearGradientBrush brush2 = new LinearGradientBrush(rect, ColorBegin, ColorEnd, Angle);
			switch (ColorStyle) {
				case PaletteColorStyle.Switch25:
					brush2.Blend = blend11;
					return brush2;

				case PaletteColorStyle.Switch33:
					brush2.Blend = blend2;
					return brush2;

				case PaletteColorStyle.Switch50:
					brush2.Blend = blend3;
					return brush2;

				case PaletteColorStyle.Switch90:
					brush2.Blend = blend4;
					return brush2;

				case PaletteColorStyle.Linear:
					return brush2;

				case PaletteColorStyle.Rounded:
					brush2.SetSigmaBellShape(1f, 1f);
					return brush2;

				case PaletteColorStyle.Rounding2:
					brush2.Blend = blend8;
					return brush2;

				case PaletteColorStyle.Rounding3:
					brush2.Blend = blend9;
					return brush2;

				case PaletteColorStyle.Rounding4:
					brush2.Blend = blend10;
					return brush2;

				case PaletteColorStyle.Sigma:
					brush2.SetSigmaBellShape(0.5f);
					return brush2;

				case PaletteColorStyle.HalfCut:
					brush2.Blend = blend5;
					return brush2;

				case PaletteColorStyle.QuarterPhase:
					brush2.Blend = blend6;
					return brush2;

				case PaletteColorStyle.OneNote:
					brush2.Blend = blend7;
					return brush2;

				case PaletteColorStyle.Status:
					brush2.Blend = blend12;
					return brush2;
			}
			return brush2;
		}

		public static Color GetDarkerColor(Color clr) {
			Color color = new Color();
			int red = clr.R - 0x12;
			int green = clr.G - 0x12;
			int blue = clr.B - 0x12;
			if (red < 0) {
				red = 0;
			}
			if (green < 0) {
				green = 0;
			}
			if (blue < 0) {
				blue = 0;
			}
			return Color.FromArgb(red, green, blue);
		}

		public static Color GetDarkerColor(Color clr, int amount) {
			Color color = new Color();
			int red = clr.R - amount;
			int green = clr.G - amount;
			int blue = clr.B - amount;
			if (red < 0) {
				red = 0;
			}
			if (green < 0) {
				green = 0;
			}
			if (blue < 0) {
				blue = 0;
			}
			return Color.FromArgb(red, green, blue);
		}

		public static Color GetLighterColor(Color clr) {
			int red = clr.R + 0x12;
			int green = clr.G + 0x12;
			int blue = clr.B + 0x12;
			if (red > 0xff) {
				red = 0xff;
			}
			if (green > 0xff) {
				green = 0xff;
			}
			if (blue > 0xff) {
				blue = 0xff;
			}
			return Color.FromArgb(red, green, blue);
		}

		public static Color GetLighterColor(Color clr, int amount) {
			int red = clr.R + amount;
			int green = clr.G + amount;
			int blue = clr.B + amount;
			if (red > 0xff) {
				red = 0xff;
			}
			if (green > 0xff) {
				green = 0xff;
			}
			if (blue > 0xff) {
				blue = 0xff;
			}
			return Color.FromArgb(red, green, blue);
		}

		public static Color GetModifiedColor(Color clr, int britness, int saturation, int hue) {
			Color color = new Color();
			ColorHandler.RGB rGB = new ColorHandler.RGB(clr.R, clr.G, clr.B);
			ColorHandler.HSV hSV = ColorHandler.RGBtoHSV(rGB);
			hSV.value += britness;
			hSV.Saturation += saturation;
			hSV.Hue += hue;
			rGB = ColorHandler.HSVtoRGB(hSV);
			int red = rGB.Red;
			int green = rGB.Green;
			int blue = rGB.Blue;
			if (red > 0xff) {
				red = 0xff;
			}
			if (green > 0xff) {
				green = 0xff;
			}
			if (blue > 0xff) {
				blue = 0xff;
			}
			if (red < 0) {
				red = 0;
			}
			if (green < 0) {
				green = 0;
			}
			if (blue < 0) {
				blue = 0;
			}
			return Color.FromArgb(red, green, blue);
		}

		public static PointF[] GetPoligonPointsFromPath(GraphicsPath path) {
			return path.PathPoints;
		}

		public static GraphicsPath GetRoundedBottomPath(Rectangle bounds, int radius, TabAppearance Appearance, TabHeaderStatus Status, int allowSelectedTabHighSize) {
			int x = bounds.X;
			int y = bounds.Y;
			int width = bounds.Width;
			int height = bounds.Height;
			GraphicsPath path = new GraphicsPath();
			if ((Appearance == TabAppearance.Normal) && ((Status == TabHeaderStatus.Selected) || (Status == TabHeaderStatus.HotSelected))) {
				path.AddLine(x, y, x + width, y);
				path.AddArc((x + width) - radius, (y + height) - radius, radius, radius, 0f, 90f);
				path.AddLine((int)((x + width) - radius), (int)(y + height), (int)(x + radius), (int)(y + height));
				path.AddArc(x, (y + height) - radius, radius, radius, 90f, 90f);
			} else {
				path.AddLine(x, y, x + width, y);
				path.AddArc((x + width) - radius, ((y + height) - radius) - allowSelectedTabHighSize, radius, radius, 0f, 90f);
				path.AddLine((int)((x + width) - radius), (int)((y + height) - allowSelectedTabHighSize), (int)(x + radius), (int)((y + height) - allowSelectedTabHighSize));
				path.AddArc(x, ((y + height) - radius) - allowSelectedTabHighSize, radius, radius, 90f, 90f);
			}
			path.CloseFigure();
			return path;
		}

		public static GraphicsPath GetRoundedLeftPath(Rectangle bounds, int radius) {
			GraphicsPath path = new GraphicsPath();
			path.AddRectangle(bounds);
			return path;
		}

		public static GraphicsPath GetRoundedRightPath(Rectangle bounds, int radius) {
			GraphicsPath path = new GraphicsPath();
			path.AddRectangle(bounds);
			return path;
		}

		public static GraphicsPath GetRoundedSquarePath(Rectangle bounds, int radius) {
			int x = bounds.X;
			int y = bounds.Y;
			int width = bounds.Width;
			int height = bounds.Height;
			GraphicsPath path = new GraphicsPath();
			path.AddArc(x, y, radius, radius, 180f, 90f);
			path.AddArc((x + width) - radius, y, radius, radius, 270f, 90f);
			path.AddArc((x + width) - radius, (y + height) - radius, radius, radius, 0f, 90f);
			path.AddArc(x, (y + height) - radius, radius, radius, 90f, 90f);
			path.CloseFigure();
			return path;
		}

		public static GraphicsPath GetRoundedTopPath(Rectangle bounds, int radius, TabAppearance Appearance, TabHeaderStatus Status, int allowSelectedTabHighSize) {
			int x = bounds.X;
			int y = bounds.Y;
			int width = bounds.Width;
			int height = bounds.Height;
			GraphicsPath path = new GraphicsPath();
			if ((Appearance == TabAppearance.Normal) && ((Status == TabHeaderStatus.Selected) || (Status == TabHeaderStatus.HotSelected))) {
				path.AddArc(x, y, radius, radius, 180f, 90f);
				path.AddLine(x + radius, y, ((x + width) - radius) - radius, y);
				path.AddArc((x + width) - radius, y, radius, radius, 270f, 90f);
				path.AddLine(x + width, y + height, x, y + height);
			} else {
				path.AddArc(x, y + allowSelectedTabHighSize, radius, radius, 180f, 90f);
				path.AddLine((int)(x + radius), (int)(y + allowSelectedTabHighSize), (int)(((x + width) - radius) - radius), (int)(y + allowSelectedTabHighSize));
				path.AddArc((x + width) - radius, y + allowSelectedTabHighSize, radius, radius, 270f, 90f);
				path.AddLine(x + width, y + height, x, y + height);
			}
			path.CloseFigure();
			return path;
		}

		public static Point[] GetSquaredBottomPoints(Rectangle recBounds, int cornerWidth, int cornerLeftWidth, int cornerRightWidth, TabAppearance Appearance, TabHeaderStatus Status, int allowSelectedTabHighSize) {
			Point[] pointArray = new Point[7];
			pointArray[0] = new Point(recBounds.Left, recBounds.Top);
			pointArray[1] = new Point(recBounds.Right, recBounds.Top);
			pointArray[2] = new Point(recBounds.Right, (recBounds.Bottom - cornerRightWidth) - allowSelectedTabHighSize);
			pointArray[3] = new Point(recBounds.Right - cornerRightWidth, recBounds.Bottom - allowSelectedTabHighSize);
			pointArray[4] = new Point(recBounds.Left + cornerLeftWidth, recBounds.Bottom - allowSelectedTabHighSize);
			pointArray[5] = new Point(recBounds.Left, (recBounds.Bottom - cornerLeftWidth) - allowSelectedTabHighSize);
			if ((Appearance == TabAppearance.Normal) && ((Status == TabHeaderStatus.Selected) || (Status == TabHeaderStatus.HotSelected))) {
				pointArray[2] = new Point(pointArray[2].X, pointArray[2].Y + allowSelectedTabHighSize);
				pointArray[3] = new Point(pointArray[3].X, pointArray[3].Y + allowSelectedTabHighSize);
				pointArray[4] = new Point(pointArray[4].X, pointArray[4].Y + allowSelectedTabHighSize);
				pointArray[5] = new Point(pointArray[5].X, pointArray[5].Y + allowSelectedTabHighSize);
			}
			pointArray[6] = new Point(recBounds.Left, recBounds.Top);
			return pointArray;
		}

		public static Point[] GetSquaredLeftPoints(Rectangle recBounds, int cornerWidth, int cornerLeftWidth, int cornerRightWidth, TabAppearance Appearance, TabHeaderStatus Status, int allowSelectedTabHighSize) {
			return new Point[] { new Point(recBounds.Right, recBounds.Top), new Point(recBounds.Right, recBounds.Bottom), new Point(recBounds.Left + cornerWidth, recBounds.Bottom), new Point(recBounds.Left, recBounds.Bottom - cornerWidth), new Point(recBounds.Left, recBounds.Top + cornerWidth), new Point(recBounds.Left + cornerWidth, recBounds.Top), new Point(recBounds.Right, recBounds.Top) };
		}

		public static Point[] GetSquaredRightPoints(Rectangle recBounds, int cornerWidth, int cornerLeftWidth, int cornerRightWidth, TabAppearance Appearance, TabHeaderStatus Status, int allowSelectedTabHighSize) {
			return new Point[] { new Point(recBounds.Left, recBounds.Top), new Point(recBounds.Left, recBounds.Bottom), new Point(recBounds.Right - cornerWidth, recBounds.Bottom), new Point(recBounds.Right, recBounds.Bottom - cornerWidth), new Point(recBounds.Right, recBounds.Top + cornerWidth), new Point(recBounds.Right - cornerWidth, recBounds.Top), new Point(recBounds.Left, recBounds.Top) };
		}

		public static Point[] GetSquaredTopPoints(Rectangle recBounds, int cornerWidth, int cornerLeftWidth, int cornerRightWidth, TabAppearance Appearance, TabHeaderStatus Status, int allowSelectedTabHighSize) {
			Point[] pointArray = new Point[7];
			pointArray[0] = new Point(recBounds.Left, recBounds.Bottom);
			pointArray[1] = new Point(recBounds.Left, (recBounds.Top + cornerLeftWidth) + allowSelectedTabHighSize);
			pointArray[2] = new Point(recBounds.Left + cornerLeftWidth, recBounds.Top + allowSelectedTabHighSize);
			pointArray[3] = new Point(recBounds.Right - cornerRightWidth, recBounds.Top + allowSelectedTabHighSize);
			pointArray[4] = new Point(recBounds.Right, (recBounds.Top + cornerRightWidth) + allowSelectedTabHighSize);
			if ((Appearance == TabAppearance.Normal) && ((Status == TabHeaderStatus.Selected) || (Status == TabHeaderStatus.HotSelected))) {
				pointArray[1] = new Point(pointArray[1].X, pointArray[1].Y - allowSelectedTabHighSize);
				pointArray[2] = new Point(pointArray[2].X, pointArray[2].Y - allowSelectedTabHighSize);
				pointArray[3] = new Point(pointArray[3].X, pointArray[3].Y - allowSelectedTabHighSize);
				pointArray[4] = new Point(pointArray[4].X, pointArray[4].Y - allowSelectedTabHighSize);
			}
			pointArray[5] = new Point(recBounds.Right, recBounds.Bottom);
			pointArray[6] = new Point(recBounds.Left, recBounds.Bottom);
			return pointArray;
		}

		public static Color GetSystemDarkerColor(Color clr) {
			return ControlPaint.Dark(clr);
		}

		public static Color GetSystemDarkerDarkColor(Color clr) {
			return ControlPaint.DarkDark(clr);
		}

		public static Color GetSystemLighterColor(Color clr) {
			return ControlPaint.Light(clr);
		}

		public static Color GetSystemLighterLightColor(Color clr) {
			return ControlPaint.LightLight(clr);
		}

		public static GraphicsPath GetTabRoundedPath(Rectangle bounds, int radius, TabAlignment orientation, bool IsForBorder, TabHeaderStatus Status, TabAppearance Appearance, int allowSelectedTabHighSize) {
			GraphicsPath path = new GraphicsPath();
			switch (orientation) {
				case TabAlignment.Top:
					if (IsForBorder) {
						bounds.Offset(1, 1);
						bounds.Width -= 2;
						if ((Status != TabHeaderStatus.HotSelected) && (Status != TabHeaderStatus.Selected)) {
							bounds.Height -= 2;
						}
					}
					return GetRoundedTopPath(bounds, radius, Appearance, Status, allowSelectedTabHighSize);

				case TabAlignment.Bottom:
					if (!IsForBorder) {
						goto Label_00A4;
					}
					if ((Status == TabHeaderStatus.HotSelected) || (Status == TabHeaderStatus.Selected)) {
						bounds.Offset(1, -1);
						break;
					}
					bounds.Offset(1, 1);
					bounds.Height -= 2;
					break;

				case TabAlignment.Left:
					if (IsForBorder) {
						bounds.Offset(1, 1);
						bounds.Height -= 2;
						if ((Status != TabHeaderStatus.HotSelected) && (Status != TabHeaderStatus.Selected)) {
							bounds.Width -= 2;
						}
					}
					return GetRoundedLeftPath(bounds, radius);

				case TabAlignment.Right:
					if (IsForBorder) {
						if ((Status == TabHeaderStatus.HotSelected) || (Status == TabHeaderStatus.Selected)) {
							bounds.Offset(-1, 1);
						} else {
							bounds.Offset(1, 1);
							bounds.Width -= 2;
						}
						bounds.Height -= 2;
					}
					return GetRoundedRightPath(bounds, radius);

				default:
					return path;
			}
			bounds.Width -= 2;
		Label_00A4:
			return GetRoundedBottomPath(bounds, radius, Appearance, Status, allowSelectedTabHighSize);
		}

		public static Point[] GetTabSquaredPoints(Rectangle bounds, int cornerWidth, TabAlignment orientation, int cornerLeftWidth, int cornerRightWidth, TabAppearance Appearance, TabHeaderStatus Status, int allowSelectedTabHighSize, bool IsForBorder) {
			Point[] pointArray = new Point[7];
			switch (orientation) {
				case TabAlignment.Top:
					if (IsForBorder) {
						bounds.Offset(1, 1);
						bounds.Width -= 2;
						if ((Status != TabHeaderStatus.HotSelected) && (Status != TabHeaderStatus.Selected)) {
							bounds.Height -= 2;
						}
					}
					return GetSquaredTopPoints(bounds, cornerWidth, cornerLeftWidth, cornerRightWidth, Appearance, Status, allowSelectedTabHighSize);

				case TabAlignment.Bottom:
					if (!IsForBorder) {
						goto Label_00AA;
					}
					if ((Status == TabHeaderStatus.HotSelected) || (Status == TabHeaderStatus.Selected)) {
						bounds.Offset(1, -1);
						break;
					}
					bounds.Offset(1, 1);
					bounds.Height -= 2;
					break;

				case TabAlignment.Left:
					if (IsForBorder) {
						bounds.Offset(1, 1);
						bounds.Height -= 2;
						if ((Status != TabHeaderStatus.HotSelected) && (Status != TabHeaderStatus.Selected)) {
							bounds.Width -= 2;
						}
					}
					return GetSquaredLeftPoints(bounds, cornerWidth, cornerLeftWidth, cornerRightWidth, Appearance, Status, allowSelectedTabHighSize);

				case TabAlignment.Right:
					if (IsForBorder) {
						if ((Status == TabHeaderStatus.HotSelected) || (Status == TabHeaderStatus.Selected)) {
							bounds.Offset(-1, 1);
						} else {
							bounds.Offset(1, 1);
							bounds.Width -= 2;
						}
						bounds.Height -= 2;
					}
					return GetSquaredRightPoints(bounds, cornerWidth, cornerLeftWidth, cornerRightWidth, Appearance, Status, allowSelectedTabHighSize);

				default:
					return pointArray;
			}
			bounds.Width -= 2;
		Label_00AA:
			return GetSquaredBottomPoints(bounds, cornerWidth, cornerLeftWidth, cornerRightWidth, Appearance, Status, allowSelectedTabHighSize);
		}

		public enum CornerType {
			Rounded,
			Squared
		}

		public enum PaletteColorStyle {
			Default,
			Solid,
			Switch25,
			Switch33,
			Switch50,
			Switch90,
			Linear,
			Rounded,
			Rounding2,
			Rounding3,
			Rounding4,
			Sigma,
			HalfCut,
			QuarterPhase,
			OneNote,
			Status
		}

		public enum TabHeaderStatus {
			Normal,
			NormalPreserve,
			Hot,
			Selected,
			HotSelected
		}

		public enum VisualOrientation {
			Top,
			Bottom,
			Left,
			Right
		}
	}


	internal class Win32 {
		public const int GW_CHILD = 5;
		public const int GW_HWNDFIRST = 0;
		public const int GW_HWNDLAST = 1;
		public const int GW_HWNDNEXT = 2;
		public const int GW_HWNDPREV = 3;
		public const int GW_OWNER = 4;
		public const int GWL_WNDPROC = -4;
		public const int HC_ACTION = 0;
		public const int WH_CALLWNDPROC = 4;
		public const int WM_CREATE = 1;
		public const int WM_DESTROY = 2;
		public const int WM_ERASEBKGND = 20;
		public const int WM_HSCROLL = 0x114;
		public const int WM_NCCALCSIZE = 0x83;
		public const int WM_NCCREATE = 0x81;
		public const int WM_NCHITTEST = 0x84;
		public const int WM_NCPAINT = 0x85;
		public const int WM_PAINT = 15;
		public const int WM_PARENTNOTIFY = 0x210;
		public const int WM_PRINT = 0x317;
		public const int WM_SHARED_MENU = 0x1e2;
		public const int WM_SHOWWINDOW = 0x18;
		public const int WM_WINDOWPOSCHANGING = 70;

		[DllImport("User32.dll", CharSet = CharSet.Auto)]
		public static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);
		[DllImport("Gdi32.dll", CharSet = CharSet.Auto)]
		public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
		[DllImport("User32.dll", CharSet = CharSet.Auto)]
		public static extern int GetClassName(IntPtr hwnd, char[] className, int maxCount);
		[DllImport("user32", CharSet = CharSet.Auto)]
		public static extern int GetClientRect(IntPtr hwnd, ref RECT lpRect);
		[DllImport("user32", CharSet = CharSet.Auto)]
		public static extern int GetClientRect(IntPtr hwnd, [In, Out] ref Rectangle rect);
		[DllImport("user32.dll")]
		public static extern IntPtr GetDCEx(IntPtr hwnd, IntPtr hrgnclip, uint fdwOptions);
		[DllImport("User32.dll", CharSet = CharSet.Auto)]
		public static extern IntPtr GetWindow(IntPtr hwnd, int uCmd);
		[DllImport("User32.dll", CharSet = CharSet.Auto)]
		public static extern IntPtr GetWindowDC(IntPtr handle);
		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		internal static extern bool GetWindowRect(IntPtr hWnd, [In, Out] ref Rectangle rect);
		[DllImport("user32", CharSet = CharSet.Auto)]
		public static extern bool InvalidateRect(IntPtr hwnd, ref Rectangle rect, bool bErase);
		[DllImport("User32.dll", CharSet = CharSet.Auto)]
		public static extern bool IsWindowVisible(IntPtr hwnd);
		[DllImport("user32", CharSet = CharSet.Auto)]
		public static extern bool MoveWindow(IntPtr hwnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
		[DllImport("User32", CallingConvention = CallingConvention.Cdecl)]
		public static extern int RealGetWindowClass(IntPtr hwnd, StringBuilder pszType, int cchType);
		[DllImport("user32.dll")]
		public static extern int ReleaseDC(IntPtr hwnd, IntPtr hDC);
		[DllImport("user32.dll")]
		public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
		[DllImport("User32.dll", CharSet = CharSet.Auto)]
		public static extern IntPtr SetClipboardViewer(IntPtr hWnd);
		[DllImport("user32")]
		public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
		[DllImport("uxtheme.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
		public static extern int SetWindowTheme(IntPtr hWnd, int pszSubAppName, int pszSubIdList);
		[DllImport("uxtheme.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
		public static extern int SetWindowTheme(IntPtr hWnd, int pszSubAppName, string pszSubIdList);
		[DllImport("uxtheme.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
		public static extern int SetWindowTheme(IntPtr hWnd, string pszSubAppName, int pszSubIdList);
		[DllImport("uxtheme.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
		public static extern int SetWindowTheme(IntPtr hWnd, string pszSubAppName, string pszSubIdList);
		[DllImport("user32", CharSet = CharSet.Auto)]
		public static extern bool UpdateWindow(IntPtr hwnd);
		[DllImport("user32", CharSet = CharSet.Auto)]
		public static extern bool ValidateRect(IntPtr hwnd, ref Rectangle rect);

		[StructLayout(LayoutKind.Sequential)]
		public struct NCCALCSIZE_PARAMS {
			public Win32.RECT rgc;
			public Win32.WINDOWPOS wndpos;
		}

		[StructLayout(LayoutKind.Sequential)]
		public struct RECT {
			public int Left;
			public int Top;
			public int Right;
			public int Bottom;
		}

		[StructLayout(LayoutKind.Sequential)]
		public struct WINDOWPOS {
			public IntPtr hwnd;
			public IntPtr hwndAfter;
			public int x;
			public int y;
			public int cx;
			public int cy;
			public uint flags;
		}
	}


	[ToolboxBitmap(typeof(Button))]
	public class KryptonNavigatorButton : KryptonButton {
		private static IPalette _palette;
		private static PaletteRedirect _paletteRedirect;

		public KryptonNavigatorButton() {
			if (_palette != null) {
				_palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
			}
			KryptonManager.GlobalPaletteChanged += new EventHandler(this.GlobalPaletteChanged);
			_palette = KryptonManager.CurrentGlobalPalette;
			_paletteRedirect = new PaletteRedirect(_palette);
			this.AutoSize = false;
			base.Values.ExtraText = null;
			base.Values.Text = null;
			base.Values.Image = null;
			base.Values.ImageStates.ImageCheckedNormal = null;
			base.Values.ImageStates.ImageCheckedPressed = null;
			base.Values.ImageStates.ImageCheckedTracking = null;
			base.ButtonStyle = ButtonStyle.Standalone;
			base.StateNormal.Back.Color1 = _palette.ColorTable.ToolStripContentPanelGradientEnd;
			base.StateNormal.Back.Color2 = _palette.ColorTable.ToolStripContentPanelGradientEnd;
			base.StateNormal.Back.ColorStyle = PaletteColorStyle.Solid;
			base.StateNormal.Border.Color1 = _palette.ColorTable.ToolStripBorder;
			base.StateCommon.Border.Rounding = 0;
			base.Size = new Size(0x17, 0x17);
			base.StateCommon.Content.ShortText.Color1 = _palette.ColorTable.StatusStripText;
			base.StateCommon.Content.ShortText.TextH = PaletteRelativeAlign.Center;
			base.StateCommon.Content.ShortText.TextV = PaletteRelativeAlign.Center;
			base.StateCommon.Content.ShortText.Font = new Font("Marlett", 11f, FontStyle.Regular, GraphicsUnit.Point, 2);
		}

		protected override void Dispose(bool disposing) {
			if (disposing) {
				if (_palette != null) {
					_palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
					_palette = null;
				}
				KryptonManager.GlobalPaletteChanged -= new EventHandler(this.OnGlobalPaletteChanged);
			}
			base.Dispose(disposing);
		}

		private void GlobalPaletteChanged(object sender, EventArgs e) {
			if (_palette != null) {
				_palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
			}
			_palette = KryptonManager.CurrentGlobalPalette;
			_paletteRedirect.Target = _palette;
			if (_palette != null) {
				_palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
				base.StateNormal.Back.Color1 = _palette.ColorTable.ToolStripContentPanelGradientEnd;
				base.StateNormal.Back.Color2 = _palette.ColorTable.ToolStripContentPanelGradientEnd;
				base.StateNormal.Border.Color1 = _palette.ColorTable.ToolStripBorder;
				base.StateCommon.Content.ShortText.Color1 = _palette.ColorTable.StatusStripText;
			}
			base.Invalidate();
		}

		private void OnPalettePaint(object sender, PaletteLayoutEventArgs e) {
			base.Invalidate();
		}
	}



	internal class SubClass : NativeWindow {
		private bool IsSubClassed;

		public event SubClassWndProcEventHandler SubClassedWndProc;

		public SubClass(IntPtr Handle, bool _SubClass) {
			base.AssignHandle(Handle);
			this.IsSubClassed = _SubClass;
		}

		public void CallDefaultWndProc(ref Message m) {
			base.WndProc(ref m);
		}

		public int HiWord(int Number) {
			return ((Number >> 0x10) & 0xffff);
		}

		public int LoWord(int Number) {
			return (Number & 0xffff);
		}

		public int MakeLong(int LoWord, int HiWord) {
			return ((HiWord << 0x10) | (LoWord & 0xffff));
		}

		public IntPtr MakeLParam(int LoWord, int HiWord) {
			return (IntPtr)((HiWord << 0x10) | (LoWord & 0xffff));
		}

		private int OnSubClassedWndProc(ref Message m) {
			if (this.SubClassedWndProc != null) {
				return this.SubClassedWndProc(ref m);
			}
			return 0;
		}

		protected override void WndProc(ref Message m) {
			if (!this.IsSubClassed || (this.OnSubClassedWndProc(ref m) == 0)) {
				base.WndProc(ref m);
			}
		}

		public bool SubClassed {
			get {
				return this.IsSubClassed;
			}
			set {
				this.IsSubClassed = value;
			}
		}

		public delegate int SubClassWndProcEventHandler(ref Message m);
	}


	public class Utility {
		public static StringFormat GetStringFormat(ContentAlignment contentAlignment) {
			if (!Enum.IsDefined(typeof(ContentAlignment), (int)contentAlignment)) {
				throw new InvalidEnumArgumentException("contentAlignment", (int)contentAlignment, typeof(ContentAlignment));
			}
			StringFormat format = new StringFormat();
			ContentAlignment alignment = contentAlignment;
			if (alignment <= ContentAlignment.MiddleCenter) {
				switch (alignment) {
					case ContentAlignment.TopLeft:
						format.LineAlignment = StringAlignment.Near;
						format.Alignment = StringAlignment.Near;
						return format;

					case ContentAlignment.TopCenter:
						format.LineAlignment = StringAlignment.Near;
						format.Alignment = StringAlignment.Center;
						return format;

					case (ContentAlignment.TopCenter | ContentAlignment.TopLeft):
						return format;

					case ContentAlignment.TopRight:
						format.LineAlignment = StringAlignment.Near;
						format.Alignment = StringAlignment.Far;
						return format;

					case ContentAlignment.MiddleLeft:
						format.LineAlignment = StringAlignment.Center;
						format.Alignment = StringAlignment.Near;
						return format;

					case ContentAlignment.MiddleCenter:
						format.LineAlignment = StringAlignment.Center;
						format.Alignment = StringAlignment.Center;
						return format;
				}
				return format;
			}
			if (alignment <= ContentAlignment.BottomLeft) {
				switch (alignment) {
					case ContentAlignment.MiddleRight:
						format.LineAlignment = StringAlignment.Center;
						format.Alignment = StringAlignment.Far;
						return format;

					case ContentAlignment.BottomLeft:
						format.LineAlignment = StringAlignment.Far;
						format.Alignment = StringAlignment.Near;
						return format;
				}
				return format;
			}
			switch (alignment) {
				case ContentAlignment.BottomCenter:
					format.LineAlignment = StringAlignment.Far;
					format.Alignment = StringAlignment.Center;
					return format;

				case ContentAlignment.BottomRight:
					format.LineAlignment = StringAlignment.Far;
					format.Alignment = StringAlignment.Far;
					return format;
			}
			return format;
		}

		public static bool IsVista() {
			return (Environment.OSVersion.Version.Major >= 6);
		}
	}

	public class ColorHandler {
		public static Color HSVtoColor(HSV hsv) {
			RGB rgb = HSVtoRGB(hsv);
			return Color.FromArgb(rgb.Red, rgb.Green, rgb.Blue);
		}

		public static Color HSVtoColor(int H, int S, int V) {
			return HSVtoColor(new HSV(H, S, V));
		}

		public static RGB HSVtoRGB(HSV HSV) {
			double num4 = 0.0;
			double num5 = 0.0;
			double num6 = 0.0;
			double num = ((((double)HSV.Hue) / 255.0) * 360.0) % 360.0;
			double num2 = ((double)HSV.Saturation) / 255.0;
			double num3 = ((double)HSV.value) / 255.0;
			if (num2 == 0.0) {
				num4 = num3;
				num5 = num3;
				num6 = num3;
			} else {
				double d = num / 60.0;
				int num11 = (int)Math.Floor(d);
				double num10 = d - num11;
				double num7 = num3 * (1.0 - num2);
				double num8 = num3 * (1.0 - (num2 * num10));
				double num9 = num3 * (1.0 - (num2 * (1.0 - num10)));
				switch (num11) {
					case 0:
						num4 = num3;
						num5 = num9;
						num6 = num7;
						goto Label_014D;

					case 1:
						num4 = num8;
						num5 = num3;
						num6 = num7;
						goto Label_014D;

					case 2:
						num4 = num7;
						num5 = num3;
						num6 = num9;
						goto Label_014D;

					case 3:
						num4 = num7;
						num5 = num8;
						num6 = num3;
						goto Label_014D;

					case 4:
						num4 = num9;
						num5 = num7;
						num6 = num3;
						goto Label_014D;

					case 5:
						num4 = num3;
						num5 = num7;
						num6 = num8;
						goto Label_014D;
				}
			}
		Label_014D:
			return new RGB((int)(num4 * 255.0), (int)(num5 * 255.0), (int)(num6 * 255.0));
		}

		public static RGB HSVtoRGB(int H, int S, int V) {
			return HSVtoRGB(new HSV(H, S, V));
		}

		public static HSV RGBtoHSV(RGB RGB) {
			double num7;
			double num8;
			double num4 = ((double)RGB.Red) / 255.0;
			double num5 = ((double)RGB.Green) / 255.0;
			double num6 = ((double)RGB.Blue) / 255.0;
			double num = Math.Min(Math.Min(num4, num5), num6);
			double num2 = Math.Max(Math.Max(num4, num5), num6);
			double num9 = num2;
			double num3 = num2 - num;
			if ((num2 == 0.0) || (num3 == 0.0)) {
				num8 = 0.0;
				num7 = 0.0;
			} else {
				num8 = num3 / num2;
				if (num4 == num2) {
					num7 = (num5 - num6) / num3;
				} else if (num5 == num2) {
					num7 = 2.0 + ((num6 - num4) / num3);
				} else {
					num7 = 4.0 + ((num4 - num5) / num3);
				}
			}
			num7 *= 60.0;
			if (num7 < 0.0) {
				num7 += 360.0;
			}
			return new HSV((int)((num7 / 360.0) * 255.0), (int)(num8 * 255.0), (int)(num9 * 255.0));
		}

		[StructLayout(LayoutKind.Sequential)]
		public struct HSV {
			public int Hue;
			public int Saturation;
			public int value;
			public HSV(int H, int S, int V) {
				this.Hue = H;
				this.Saturation = S;
				this.value = V;
			}

			public override string ToString() {
				return string.Format("({0}, {1}, {2})", this.Hue, this.Saturation, this.value);
			}
		}

		[StructLayout(LayoutKind.Sequential)]
		public struct RGB {
			public int Red;
			public int Green;
			public int Blue;
			public RGB(int R, int G, int B) {
				this.Red = R;
				this.Green = G;
				this.Blue = B;
			}

			public override string ToString() {
				return string.Format("({0}, {1}, {2})", this.Red, this.Green, this.Blue);
			}
		}
	}

}
